package zcw.com.lib_leet_code;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 朱城委 on 2021/7/14.<br><br>
 *
 * 反转字符串中的元音字母
 */
public class Topic345 {
    public static void main(String[] args) {
        Topic345 instance = new Topic345();

        String string = "hello";
        System.out.println(instance.reverseVowels(string));

        string = "leetcode";
        System.out.println(instance.reverseVowels(string));

        string = " ";
        System.out.println(instance.reverseVowels(string));

        string = "aA";
        System.out.println(instance.reverseVowels(string));

        string = "a.b,.";
        System.out.println(instance.reverseVowels(string));
    }

    public String reverseVowels(String s) {
        boolean[] template = new boolean[128];
        char[] vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        for(char ch : vowels) {
            template[ch] = true;
        }

        char[] result = s.toCharArray();
        int low = 0;
        int high = s.length() - 1;
        while (low < high) {
            while (low < high && !template[s.charAt(low)]) {
                low++;
            }

            while (low < high && !template[s.charAt(high)]) {
                high--;
            }

            result[low] = s.charAt(high);
            result[high] = s.charAt(low);
            low++;
            high--;
        }

        return String.valueOf(result);
    }

    public String reverseVowels2(String s) {
        char[] result = s.toCharArray();
        Set<Character> set = new HashSet<>();
        set.add('a');
        set.add('e');
        set.add('i');
        set.add('o');
        set.add('u');
        set.add('A');
        set.add('E');
        set.add('I');
        set.add('O');
        set.add('U');

        int low = 0;
        int high = s.length() - 1;
        while (low < high) {
            while (low < high && !set.contains(s.charAt(low))) {
                low++;
            }

            while (low < high && !set.contains(s.charAt(high))) {
                high--;
            }

            result[low] = s.charAt(high);
            result[high] = s.charAt(low);
            low++;
            high--;
        }

        return String.valueOf(result);
    }
}
